//
//  SwitchSetViewController.m
//  Lightthings
//
//  Created by 王岩 on 2024/11/29.
//

#import "SwitchSetViewController.h"

@interface SwitchSetViewController ()
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UISwitch *switchBtn;
@property (nonatomic, assign) DeviceCommandEnum action;

@end

@implementation SwitchSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleLab.text = SWString(@"On/Off");

    NSString * uuid = self.metaInfo[@"uuid"];
    NSArray * list = [FRQBluetoothManager shareManager].curPeripherals;
    for (CBPeripheral * per in list) {
        if ([per.identifier.UUIDString isEqualToString:uuid]) {
            self.peripheral = per;
        }
    }
    self.action = [self.metaInfo[@"action"] intValue];
    if (self.action == Command_Antilost) {
        self.jy_title = SWString(@"防丢设置");
    }else  if (self.action == Command_FindPhone) {
        self.jy_title = SWString(@"查找手机");
    }
    
    [SWInitializer jy_initRedButtonWithTarget:self action:@selector(clickSureBtnAction) frame:CGRectMake(0, 0, kScreenWidth - 100, 50) title:SWString(@"Confirm") on:self.view layout:^(MASConstraintMaker * _Nonnull make) {
        kMakeLV(50);
        kMakeBV(-100);
        kMakeWHV(kScreenWidth-100, 50);
    }];
    
    
    [self readData];
    // Do any additional setup after loading the view from its nib.
}

- (void)readData {
    [[WatchCommandTool sharedInstance] readStormCommand:self.action ToPeripheral:self.peripheral reBack:^(NSString * _Nonnull backData) {
        if (backData) {
            NSString * valueStr = [backData substringWithRange:NSMakeRange(26, 2)];
            unsigned long value = strtoll([[NSString stringWithFormat:@"0x%@",valueStr] UTF8String], 0, 16);
            NSLog(@"开关数值：%lu",value);
          
            self.switchBtn.on = value;
            
            
        }
    }];
}

- (IBAction)clickSwitchBtn:(UISwitch *)sender {
    
}

-(void)clickSureBtnAction {
    int value = self.switchBtn.isOn;
    unsigned char byte1 = (value & 0xff);

    [[WatchCommandTool sharedInstance] writeStormCommand:self.action ToPeripheral:self.peripheral Value:@[@(byte1)] reBack:^(NSString * _Nonnull backData) {
        if (backData) {
            if ([backData hasSuffix:@"00"]) {
                toast(SWString(@"Success"));
            }else {
                toast(SWString(@""));
            }
//            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
